<template>
  <div>
<van-nav-bar
  title="上架众筹"
  left-arrow
  @click-left="onClickLeft"
/>

<van-form @submit="onSubmit">
  <van-cell-group inset>
    <van-field
      v-model="crfuname"
      name="众筹名称"
      label="众筹名称"
      placeholder="众筹名称"
      :rules="[{ required: true, message: '请填写众筹名称' }]"
    />
    <van-field
      v-model="crfuprice"
      name="众筹价格"
      label="众筹价格"
      placeholder="众筹价格"
      :rules="[{ required: true, message: '请填写众筹价格' }]"
    />
    <van-field
      v-model="crfugift"
      name="赠品"
      label="赠品"
      placeholder="赠品（选填）"
      :rules="[{ message: '请填写赠品' }]"
    />

    <van-cell title="选择截至日期" :value="date" @click="show = true" />
    <van-calendar  v-model:show="show" @confirm="onConfirm" :show-confirm="false" />

    <van-field name="uploader" label="图片上传">
      <template #input>
        <van-uploader v-model="value" />
      </template>
    </van-field>

  </van-cell-group>
  <div style="margin: 16px;">
    <van-button round block type="primary" @click="btn" native-type="submit">
      提交
    </van-button>
  </div>
</van-form>

  </div>
</template>

<script>
import { ref } from 'vue';
import axios from 'axios';
import router from '@/router';
  export default {
    data() {
      return {
        sh: [],
      }
    },
    setup() {
      const onClickLeft = () => history.back();
      const crfuname = ref('');
      const crfuprice = ref('');
      const crfugift = ref('');
      const date = ref('');
      const show = ref(false);
      const onSubmit = (values) => {
        // console.log('submit', values);
      };
      
      const formatDate = (date) => `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} 00:00:00`;
      const onConfirm = (value) => {
        show.value = false;
        date.value = formatDate(value);
        value = date.value;
        console.log(value);
      };

      const value = ref([
      //  { url: 'https://fastly.jsdelivr.net/npm/@vant/assets/leaf.jpeg' },
      ]);

      return {
        onClickLeft,
        crfuname,
        crfuprice,
        crfugift,
        onSubmit,
        date,
        show,
        onConfirm,
        value,
      };
    },
    created() {
      axios.get('http://localhost:8087/crowdfunding/selectAll').then((res)=>{
          this.wenzhs = res.data.data
          console.log(this.wenzhs.length)
      })
    },
    methods:{
      btn() {
        axios.post("http://localhost:8087/crowdfunding/saveCrowdfunding", {
          crowdfundingId: Math.random()*1000000,
          crowdfundingName: this.crfuname,
          crowdfundingPrice: this.crfuprice,
          crowdfundingDeadline: this.date,
          crowdfundingGift: this.crfugift,
        }).then(function() {
          router.push("/ms")
        }).catch(function(error) {
          console.log(error);
        })
      }
    },
  };

</script>

<style lang="scss" scoped>

</style>